package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityAdminDao;
import com.java.domain.ActivityAdmin;
import com.java.domain.event.ActivityAdminEvent;
import com.java.service.AdminService;
import com.java.util.MyCons;
import com.java.util.MyTools;
import com.java.util.interceptor.LoginUser;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class AdminServiceImpl implements AdminService {
	@Resource
	private ActivityAdminDao adminDao;

	@Override
	public BaseRespone login(String account, String password) {
		ActivityAdminEvent info = adminDao.selectByInfo(new ActivityAdmin().setAdminAccount(account));
		if (null == info) {
			return BaseRespone.error("Account error");
		}
		if(!MyTools.md5Match(password, info.getAdminPassword())) {
			return BaseRespone.error("Password error");
		}
		LoginUser user = new LoginUser(info.getAdminId(), account, info.getAdminName(), null, info);
		ServletUtil.getSession().setAttribute(MyCons.ADMIN_ROLE, user);
		return BaseRespone.success("Success");
	}

	@Override
	public TableRespone getList(int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<ActivityAdminEvent> list = adminDao.selectAll(params, page, size);
		long total = adminDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public ActivityAdminEvent getOne(String adminId) {
		ActivityAdminEvent info = adminDao.selectByKey(adminId);
		return info;
	}

	@Override
	public BaseRespone saveAdmin(ActivityAdmin admin) {
		int row = 0;
		ActivityAdmin info = adminDao.selectByKey(admin.getAdminId());
		if(null == info) {
			ActivityAdmin has = adminDao.selectByInfo(new ActivityAdmin().setAdminAccount(admin.getAdminAccount()));
			if(null != has) {
				return BaseRespone.error("Account already exists");
			}
			info = new ActivityAdmin();
			info.setAdminId(MyTools.getUUID());
			info.setAdminAccount(admin.getAdminAccount());
			info.setAdminName(admin.getAdminName());
			info.setAdminMobile(admin.getAdminMobile());
			info.setAdminPassword(MyTools.getMd5(admin.getAdminPassword()));
			info.setAdminTime(new Date());
			row = adminDao.insertSelective(info);
		}else {
			info.setAdminName(admin.getAdminName());
			info.setAdminMobile(admin.getAdminMobile());
			if(!MyTools.isEmpty(admin.getAdminPassword())) {
				info.setAdminPassword(MyTools.getMd5(admin.getAdminPassword()));
			}
			row = adminDao.updateSelective(info);
		}
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone delAdmin(String adminId) {
		long total = adminDao.selectAllCount(null);
		if(total == 1) {
			return BaseRespone.error("At least one account exists");
		}
		int row = adminDao.deleteByKey(adminId);
		return BaseRespone.result(row, "Success");
	}

}
